package fractal;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;

import entity.Position;

public class BiomeView extends BasicGame
{
	protected Position _pos;
	protected static int NBSCREEN = 128;
	protected static int TAILLECASE = 4;
	public static int DEPLACEMENT = 4;
	protected BiomeMap _Map;

	public BiomeView(String title)
	{
		super(title);
	}

	@Override
	public void render(GameContainer arg0, Graphics pGraphics) throws SlickException
	{
		// TODO Auto-generated method stub
		for (int i = _pos.getX(); i < _pos.getX() + NBSCREEN; i++)
		{
			for (int j = _pos.getY(); j < _pos.getY() + NBSCREEN; j++)
			{
				pGraphics.setColor(new Color(0, 0, 0));
				visuBiome(pGraphics, i, j);
				int x = (i - _pos.getX()) * TAILLECASE;
				int y = (j - _pos.getY()) * TAILLECASE;
				Rectangle rectangle = new Rectangle(x, y, x + TAILLECASE, y + TAILLECASE);
				pGraphics.fill(rectangle);
			}
		}
	}

	protected void visuBiome(Graphics pGraphics, int x, int y)
	{
		TerrainBiome biome = _Map.getMap()[x][y];
		BiomeType biometype = biome.getBiomeType();
		if (BiomeType.OCEAN.equals(biometype))
		{
			pGraphics.setColor(new Color(27, 75, 148));
		}
		else if (BiomeType.SEA.equals(biometype))
		{
			pGraphics.setColor(new Color(46, 115, 216));
		}
		else if (biome.getRiver())
		{
			pGraphics.setColor(new Color(0, 0, 185));
			return;
		}
		if (BiomeType.DESERT.equals(biometype))
		{
			pGraphics.setColor(new Color(223, 214, 32));
		}
		if (BiomeType.FOREST_PLUVIAL.equals(biometype))
		{
			pGraphics.setColor(new Color(66, 189, 128));
		}
		if (BiomeType.FOREST_TEMPARATE.equals(biometype))
		{
			pGraphics.setColor(new Color(56, 207, 48));
		}
		if (BiomeType.FOREST_TROPICAL.equals(biometype))
		{
			pGraphics.setColor(new Color(88, 166, 36));
		}
		if (BiomeType.SCRUBLAND.equals(biometype))
		{
			pGraphics.setColor(new Color(206, 189, 91));
		}
		if (BiomeType.STEPPE.equals(biometype))
		{
			pGraphics.setColor(new Color(162, 234, 180));
		}
		if (BiomeType.TAIGA.equals(biometype))
		{
			pGraphics.setColor(new Color(197, 197, 197));
		}
		if (BiomeType.TUNDRA.equals(biometype))
		{
			pGraphics.setColor(new Color(255, 255, 255));
		}

	}

	@Override
	public void init(GameContainer pGC) throws SlickException
	{
		//
		_pos = new Position(0, 0);
		//
		TerrainMap map = new TerrainMap(129);
		map.generateWorld();

		_Map = new BiomeMap(map);

		new GenerateRiver2().generateRivers(_Map, 5);
	}

	@Override
	public void update(GameContainer gc, int arg1) throws SlickException
	{
		Input input = gc.getInput();
		if (input.isKeyDown(Input.KEY_Z) && _pos.getY() - DEPLACEMENT >= 0)
		{
			_pos = new Position(_pos.getX(), _pos.getY() - DEPLACEMENT);
		}
		if (input.isKeyDown(Input.KEY_S) && _pos.getY() + DEPLACEMENT < _Map.getMap().length - NBSCREEN)
		{
			_pos = new Position(_pos.getX(), _pos.getY() + DEPLACEMENT);
		}
		if (input.isKeyDown(Input.KEY_Q) && _pos.getX() - DEPLACEMENT >= 0)
		{
			_pos = new Position(_pos.getX() - DEPLACEMENT, _pos.getY());
		}
		if (input.isKeyDown(Input.KEY_D) && _pos.getX() + DEPLACEMENT < _Map.getMap().length - NBSCREEN)
		{
			_pos = new Position(_pos.getX() + DEPLACEMENT, _pos.getY());
		}

	}

}
